package com.wing.ext.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wing.cms.domain.Channel;
import com.wing.common.util.GUID;
import com.wing.common.util.PageBean;
import com.wing.ext.dao.InformationDao;
import com.wing.ext.domain.Information;
import com.wing.sys.domain.Code;
/**
 * 
 * @author please write yout name
 * 
 */
@Service
@Transactional
public class InformationManager {
	@Autowired
	private InformationDao informationdao;
	
	
	/**
	 * 获得该对象所有数据的列表
	 * 
	 * @return list
	 */
	public List<Map<String,Object>> findAll(){
		return informationdao.findAll();	 
	}
	
	public PageBean findPageList(int currentPage, int pageSize, String siteId){
		return informationdao.findPageList(currentPage,pageSize,siteId);	 
	}
	
	/**
	 * 通过id得到对象
	 *
	 * @param  id
	 * @return pObject
	 */
	public Information findById(String id){
		return informationdao.findById(id);	 
	}
	
	/**
	 * 保存对象
	 *
	 * @param  information
	 * @param userId 
	 */
	public void saveInformation(Information information, String userId){
		if(information.getId() == null ||  "".equals(information.getId())){
			information.setId(new GUID().toString());
			information.setCreatetime(new Date());
			information.setCreator(userId);
			informationdao.insert(information);
		}else{
			information.setCreatetime(new Date());
			informationdao.update(information);
		}
	}

	/**
	 * 编辑对象
	 *
	 * @param  information
	 */
	public void updateInformation(Information information){
		information.setCreatetime(new Date());
		informationdao.update(information);
	}
	
	public void updateInformationContent(Information information){
		informationdao.updateInformationContent(information);
	}
	 /**
	 * 删除对象
	 *
	 * @param  chk 对象id数组
	 */
	public void deleteInformations(String[] chk) {
		for(int i=0;i<chk.length;i++){
			deleteInformations(chk[i]);
		}
	}
	public void deleteInformations(String id) {
		List list = informationdao.getChildByPid(id);
		for(int i=0;i<list.size();i++){
			Map map = (Map)list.get(i);
			deleteInformations((String)map.get("id"));
		}
		informationdao.delete(id);
	}

	public PageBean findInformationsByType(int currentPage, int pageSize,String site_id, String type_id,
			String toHome) {
		return informationdao.findInformationsByType(currentPage,pageSize,site_id,type_id,toHome);
	}

	public List getSearchIndex(String site_id, String inforField, String type_id) {
		return informationdao.getSearchIndex(site_id,inforField,type_id);
	}

	public PageBean findInformationsByTypeField(Integer currentPage, int pageSize, String site_id, String inforField,
			String type_id,String type_id2) {
		return informationdao.findInformationsByTypeField(currentPage,pageSize,site_id,inforField,type_id,type_id2);
	}

	public PageBean findHInformationsByType(int currentPage, int pageSize,
			String site_id, String type_id, String toHome) {
		return informationdao.findHInformationsByType(currentPage,pageSize,site_id,type_id,toHome);
	}

	public String createDHTMLXML(String pid, boolean firstFlag, String siteId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		if (pid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			Information information = informationdao.findById(pid);
			buffer.append("<tree id=\"0\">");
			buffer.append("<item text=\""+information.getTitle()+"\" id=\""+information.getId()+"\" open=\"1\" select=\"1\">");
		} else {
			buffer.append("<tree id=\"" + pid + "\">");
		}
		// 循环读取所有节点 
		List<Map<String, Object>> list = informationdao.findChildByID(pid,siteId);
		for (Map map : list) {
			String id = (String)map.get("id");
			String title = (String)map.get("title");
			buffer.append("<item text= \"" + title + "\"");
			buffer.append(" id= \"" + id + "\"");

			if (informationIsLeaf(id,siteId)) {
				buffer.append(" im0=\"iconText.gif\" im1=\"iconTexts.gif\" im2=\"iconText.gif\"");
				buffer.append(" child=\"0\" >");
			} else {
				buffer.append(" im0=\"folderClosed.gif\" im1=\"folderOpen.gif\" im2=\"folderClosed.gif\"");
				buffer.append(" child=\"" + id + "\">");
			}
			buffer.append("</item>");
		}
		if (pid.equalsIgnoreCase(Code.CODEROOT) || firstFlag) {
			buffer.append("</item>");
		}
		buffer.append("</tree>");
		return buffer.toString();
	}
	
	/**
	 * 判断该编码是否是叶子节点
	 * @param codeanization Id
	 * @return 
	 */
	private boolean informationIsLeaf(String id,String siteId){
		List list = informationdao.findChildByID(id,siteId);
		return list.size()<=0;
	}

	public PageBean getReceiptInformationsByPid(int currentPage, int pageSize,
			String site_id, String pid) {
		return informationdao.getReceiptInformationsByPid(currentPage,pageSize,site_id,pid);
	}
}




